package com.jinlejia.erp.ui.mine.contract;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.github.promeg.pinyinhelper.Pinyin;
import com.jinlejia.erp.R;
import com.jinlejia.erp.bean.ShareShopAndAgentBean;
import com.jinlejia.erp.databinding.ActivitySharedStoreListBinding;
import com.jinlejia.erp.ui.mine.adapter.SharedStoresAndBrokersListAdapter;
import com.jinlejia.erp.utils.PinyinComparatorShareShopAndAgent;
import com.jinlejia.erp.widget.SideBar;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 经纪人共享店筛选列表
 */
public class SharedStoreAndAgentListActivity extends AppCompatActivity {
    private ActivitySharedStoreListBinding binding;
    private ArrayList<ShareShopAndAgentBean> dataShareList;
    private ArrayList<ShareShopAndAgentBean> dataAgentList;
    private SharedStoresAndBrokersListAdapter adapter;

    /**
     * 汉字转换成拼音的类
     */
    private PinyinComparatorShareShopAndAgent pinyinComparator;  //根据拼音来排列ListView里面的数据类
    private List<ShareShopAndAgentBean> SourceDateList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_shared_store_list);
        binding.setEvent(this);
        if ((ArrayList<ShareShopAndAgentBean>) getIntent().getSerializableExtra("ShareShopdataList") != null) {
            dataShareList = (ArrayList<ShareShopAndAgentBean>) getIntent().getSerializableExtra("ShareShopdataList");
        } else if ((ArrayList<ShareShopAndAgentBean>) getIntent().getSerializableExtra("AgentdataList") != null) {
            dataAgentList = (ArrayList<ShareShopAndAgentBean>) getIntent().getSerializableExtra("AgentdataList");
        }
        initView();
        initEvent();
    }

    private void initEvent() {
        //设置右侧触摸监听
        binding.idSideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                if (adapter != null) {
                    int position = adapter.getPositionForSection(s.charAt(0));
                    if (position != -1) {
                        binding.idListView.setSelection(position);
                    }
                }
            }
        });

        binding.idListView.setOnItemClickListener((parent, view, position, id) -> {
            if (binding.tvTitle.getText().toString().equals("共享店")) {
                EventBus.getDefault().post(adapter.getItem(position), "TAG_ShareShopCode");
            } else {
                EventBus.getDefault().post(adapter.getItem(position), "TAG_AgentPing");
            }
            finish();
        });
    }

    private void initView() {
        binding.tvTitle.setText(getIntent().getStringExtra("title"));
        pinyinComparator = new PinyinComparatorShareShopAndAgent();
        if (dataShareList != null) {
            SourceDateList = filledData(dataShareList);
        } else if (dataAgentList != null) {
            SourceDateList = filledDataAgent(dataAgentList);
        }

        Collections.sort(SourceDateList, pinyinComparator);
        adapter = new SharedStoresAndBrokersListAdapter((ArrayList<ShareShopAndAgentBean>) SourceDateList, this);
        binding.idListView.setAdapter(adapter);
        if (binding.tvTitle.getText().equals("共享店")) {
            if (dataShareList.size() == 0 && dataAgentList == null) {
                binding.idTxt.setText("暂无共享店");
                binding.idEmpty.setVisibility(View.VISIBLE);
            }
        } else {
            if (dataShareList == null && dataAgentList.size() == 0) {
                binding.idTxt.setText("暂无经纪人");
                binding.idEmpty.setVisibility(View.VISIBLE);
            }
        }
    }


    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }


    /**
     * 为ListView填充数据
     *
     * @param
     * @return
     */
    private List<ShareShopAndAgentBean> filledData(ArrayList<ShareShopAndAgentBean> data) {
        List<ShareShopAndAgentBean> mSortList = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {
            ShareShopAndAgentBean clients = new ShareShopAndAgentBean();
            clients.setName(data.get(i).getName());
            clients.setCode(data.get(i).getCode());
            //汉字转换成拼音
            String pinyin = null;
            if (data.get(i).getName().substring(0, 3).equals("（新）")) {
                pinyin = Pinyin.toPinyin(data.get(i).getName().substring(3).toCharArray()[0]);
            } else {
                pinyin = Pinyin.toPinyin(data.get(i).getName().toCharArray()[0]);
            }
            String sortString = pinyin.substring(0, 1).toUpperCase();
            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                clients.setSortLetters(sortString.toUpperCase());
            } else {
                clients.setSortLetters("#");
            }

            mSortList.add(clients);
        }
        return mSortList;

    }


    /**
     * 为ListView填充数据
     *
     * @param
     * @return
     */
    private List<ShareShopAndAgentBean> filledDataAgent(ArrayList<ShareShopAndAgentBean> data) {
        List<ShareShopAndAgentBean> mSortList = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {
            ShareShopAndAgentBean clients = new ShareShopAndAgentBean();
            clients.setName(data.get(i).getName());
            clients.setCode(data.get(i).getCode());
            //汉字转换成拼音
            String pinyin = Pinyin.toPinyin(data.get(i).getName().toCharArray()[0]);
            String sortString = pinyin.substring(0, 1).toUpperCase();
            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                clients.setSortLetters(sortString.toUpperCase());
            } else {
                clients.setSortLetters("#");
            }
            mSortList.add(clients);
        }
        return mSortList;

    }


}
